﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.Model;
using Infrastructure.Pub;
using Infrastructure.Tool;
using System.Text;
using DormMS.Model.ViewModels;
using Senparc.Weixin.MP.Containers;

namespace DormMS
{
    [AuthorizeFilter]
    public class BaseController : Controller
    {
        private CacheUserInfo _userInfo;

        public IUserInfoService _userService;

        public CacheUserInfo CurrentLoginUser
        {
            get
            {
                return _userInfo;
            }
        }


        public BaseController(IUserInfoService s)
        {
            _userService = s;
            //string uid = Generator.GetCookie(PubConst.CookieUserKey);
            //if (!uid.IsNullOrEmpty())
            //{
            //    string uniqueKey = string.Format(PubConst.CacheUserKey, uid);
            //    _userInfo = CacheManager<CacheUserInfo>.GetInstance()[uniqueKey];
            //    if (!IsLogin)
            //    {
            //        _userInfo = _userService.GetUserInfo(uid.ObjToInt());
            //        CacheManager<CacheUserInfo>.GetInstance().Add(uniqueKey, _userInfo, int.MaxValue);
            //    }
            //}
            _userInfo = s.GetCacheUser();
            if (!AccessTokenContainer.CheckRegistered(_userInfo.AppId))//检查是否已经注册
            {
                AccessTokenContainer.Register(_userInfo.AppId, _userInfo.AppSecret);//如果没有注册则进行注册
            }
        }





        public bool IsLogin
        {
            get
            {
                return (_userInfo != null && _userInfo.Id > 0);
            }
        }



        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
        {
            return new CustomJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding };
        }

        public new JsonResult Json(object data, JsonRequestBehavior jsonRequest)
        {
            return new CustomJsonResult { Data = data, JsonRequestBehavior = jsonRequest };
        }

        public new JsonResult Json(object data)
        {
            return new CustomJsonResult { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

    }


    public class MyLoginAuthorize : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            string uid = Generator.GetCookie(PubConst.CookieUserKey);
            if (uid.IsNullOrEmpty())
            {
                return false;
            }
            else
            {
                string uniqueKey = string.Format(PubConst.CacheUserKey, uid);
                var user = CacheManager<CacheUserInfo>.GetInstance()[uniqueKey];
                if (user == null || user.Id <= 0)
                {
                    return false;
                }
            }

            return true;
        }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            try
            {
                string uid = Generator.GetCookie(PubConst.CookieUserKey);
                if (!uid.IsNullOrEmpty())
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            base.OnAuthorization(filterContext);
        }


    }
}